﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_NhaSach;
using BLL;

namespace BLL_NhaSach
{
    public class TheLoaiBLL : IProcessData
    {
        QLNhaSachEntities db;

        public TheLoaiBLL(string cnStr)
        {
            db = new QLNhaSachEntities(cnStr);
        }

        public object getAllData()
        {
            return (from tl in db.TheLoais select new { tl.MaLoai, tl.TenLoai });
        }

        public object findObjects(string find_content)
        {
            throw new NotImplementedException();
        }

        public object findObjects(int find_content)
        {
            return (db.TheLoais.SingleOrDefault(p => p.MaLoai == find_content));
        }

        public object sort(string Col_Name, bool state)
        {
            throw new NotImplementedException();
        }

        public bool addObject(object addObj)
        {
            TheLoai tl = (TheLoai)addObj;
            try
            {
                db.TheLoais.AddObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool deleteObject(int id)
        {
            TheLoai tl = db.TheLoais.SingleOrDefault(p => p.MaLoai == id);
            if (tl == null)
            {
                return false;
            }
            else
            {
                try
                {
                    db.TheLoais.DeleteObject(tl);
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool updateObject(object updateObj)
        {
            TheLoai tl_new = (TheLoai)updateObj;
            TheLoai tl_old = db.TheLoais.SingleOrDefault(p => p.MaLoai == tl_new.MaLoai);
            if (tl_old != null)
            {
                try
                {
                    tl_old.TenLoai = tl_new.TenLoai;
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }
        public String getName(int id)
        {
            return db.TheLoais.SingleOrDefault(p => p.MaLoai == id).TenLoai;
        }
    }
}
